//
//  YZLPostCell.m
//  Car
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLPostCell.h"
#import "YZLDiscuss.h"
#import "Macro.h"

@interface YZLPostCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *authorLabel;
@property (nonatomic, strong) UILabel *descLabel;

@end


@implementation YZLPostCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.textColor = [UIColor grayColor];
        self.dateLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.dateLabel];
        
        self.authorLabel = [[UILabel alloc] init];
        self.authorLabel.textColor = [UIColor grayColor];
        self.authorLabel.font = [UIFont systemFontOfSize:14];
        self.authorLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.authorLabel];
        
        self.descLabel = [[UILabel alloc] init];
        self.descLabel.font = [UIFont systemFontOfSize:15.5];
        self.descLabel.numberOfLines = 0;
        [self.contentView addSubview:self.descLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.isAll) {
        self.titleLabel.frame = CGRectMake(15, 0, kWidth - 30, self.frame.size.height * 2 / 3);
        self.dateLabel.frame = CGRectMake(15, self.frame.size.height * 2 / 3, kWidth / 2, self.frame.size.height / 3);
        self.authorLabel.frame = CGRectMake(kWidth / 2, self.frame.size.height * 2 / 3, kWidth / 2 - 15, self.frame.size.height / 3);
    } else {
        self.titleLabel.frame = CGRectMake(15, 0, kWidth - 30, self.contentView.frame.size.height / 4);
        self.descLabel.frame = CGRectMake(15, self.titleLabel.frame.size.height, kWidth - 30, self.contentView.frame.size.height / 2);
        self.dateLabel.frame = CGRectMake(15, self.titleLabel.frame.size.height + self.descLabel.frame.size.height, kWidth / 2, 16);
        self.authorLabel.frame = CGRectMake(kWidth / 2, self.titleLabel.frame.size.height + self.descLabel.frame.size.height, kWidth / 2 - 15, 16);
    }
    
}

- (void)setModel:(YZLDiscuss *)model {
    if (_model != model) {
        _model = model;
    }
    self.titleLabel.text = [[model.postTitle stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    self.dateLabel.text = [[NSString stringWithFormat:@"%@", [NSDate dateWithTimeIntervalSince1970:[model.postCreateTime integerValue] + 8 * 3600]] substringToIndex:10];
    self.authorLabel.text = [[model.author stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    
   
}

- (void)setSearchString:(NSString *)searchString {
    if (_searchString != searchString) {
        _searchString = searchString;
    }
    
    NSMutableAttributedString *arrTitleStr = [[NSMutableAttributedString alloc] initWithString:[[_model.postTitle stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""]];
    NSRange range = [[[_model.postTitle stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""] rangeOfString:_searchString];
    [arrTitleStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:17]} range:range];
    self.titleLabel.attributedText = arrTitleStr;
    
    NSMutableAttributedString *arrAuStr = [[NSMutableAttributedString alloc] initWithString:[[_model.author stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""]];
    NSRange rangeAu = [[[_model.author stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""] rangeOfString:_searchString];
    [arrAuStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:10]} range:rangeAu];
    self.authorLabel.attributedText = arrAuStr;
    
    NSString *decsStr = [[_model.postDesc stringByReplacingOccurrencesOfString:@"<font color=\"red\">" withString:@""] stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    NSMutableAttributedString *arrDecsStr = [[NSMutableAttributedString alloc] initWithString: decsStr];
    NSRange decsRange = [decsStr rangeOfString:_searchString];
    
    [arrDecsStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:15]} range:decsRange];
    _descLabel.attributedText = arrDecsStr;
}




- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
